import pandas as pd
import numpy as np
from torch.utils.data.dataset import Dataset

class DatasetFromCSV(Dataset):
    def __init__(self, csv_path, training_length, predicted_length,training_ratio = None, test_ratio = None, valid_index = None, last_index = None):
        self.data = pd.read_csv(csv_path)
        if last_index is not None:
            self.data = self.data[:last_index] # 先处理last_index 这样可以避免valid_index截取之后导致数据长度不一致
        if valid_index is not None:
            self.data = self.data[valid_index:] # 前面的数据有问题 截取从有效的开始索引和之后的数据
        self.data.dropna(axis=0, how='any')  # 删除表中含有任何NaN的行
        if training_ratio is not None:
            self.data = self.data.iloc[:int(self.data.size*training_ratio)]
        if test_ratio is not None:
            self.data = self.data.iloc[int(self.data.size*test_ratio):]
        self.training_length = training_length
        self.predicted_length = predicted_length
        self.labels = np.asarray(self.data.iloc[:, 0])


    def __getitem__(self, index):
        training_index = self.training_length + index
        return (self.data.values[index:training_index], self.data.values[training_index: (training_index+self.predicted_length)])

    def __len__(self):
        return len(self.data) - self.training_length - self.predicted_length
